package com.qingzhuge.manager.controller;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.qingzhuge.common.annotation.ASysLog;
import com.qingzhuge.common.enums.EResourceType;
import com.qingzhuge.common.enums.ERoleType;
import com.qingzhuge.common.validator.ValidatorUtil;
import com.qingzhuge.core.annotation.AResource;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.manager.controller.base.BaseUidPkLongController;
import com.qingzhuge.manager.dto.request.SysRoleRequestDto;
import com.qingzhuge.manager.dto.response.SysRoleResponseDto;
import com.qingzhuge.manager.entity.SysRole;
import com.qingzhuge.manager.service.ISysRoleService;
import com.qingzhuge.utils.PageUtil;
import io.swagger.annotations.Api;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.qingzhuge.common.Constants.Controller.PATH_LIST;

/**
 * @author : zero.xiao
 * @description : 角色管理
 * @date :2018/6/8 0008 下午 14:51
 * @modified :
 */
@ASysLog(value = "系统角色")
@Api(tags = "S角色管理")
@AResource(name = "角色管理", type = EResourceType.menu, catalog = "系统管理")
@RestController
@RequestMapping("api/sys/auth/role")
public class SysRoleController extends BaseUidPkLongController<
        SysRole, ISysRoleService, SysRoleRequestDto, SysRoleResponseDto> {

    @Override
    @GetMapping(PATH_LIST)
    public ResponseEntity<ResponseBodyDto<List<SysRoleResponseDto>>> queryList(@RequestParam(required = false) SysRoleRequestDto params) {
        List<SysRole> list = service.findList();
        List<SysRoleResponseDto> resList = Lists.newArrayList();
        list.forEach(t -> resList.add(service.bean2Dto(t, SysRoleResponseDto.class)));
        return returnSuccess(resList);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<PageUtil<SysRoleResponseDto>>> queryPage(@RequestBody(required = false) SysRoleRequestDto param) {
        return super.queryPage(param);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysRoleResponseDto>> get(@PathVariable Long id) {
        return super.get(id);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysRoleResponseDto>> save(@RequestBody SysRoleRequestDto role) {
        return super.update(role);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<SysRoleResponseDto>> update(@RequestBody SysRoleRequestDto role) {
        ValidatorUtil.validateEntity(role);
        return super.update(role);
    }

    @Override
    public ResponseEntity<ResponseBodyDto<Object>> delete(@PathVariable Long id) {
        return super.delete(id);
    }

    @GetMapping("type")
    public ResponseEntity<ResponseBodyDto<List<Map<String, Object>>>> type() {
        Map<String, Object> map = ERoleType.map();
        List<Map<String, Object>> type = Lists.newArrayList();
        map.forEach((k, v) -> {

            Map<String, Object> ut = Maps.newHashMap();
            ut.put("name", v);
            ut.put("value", k);
            type.add(ut);
        });
        return returnSuccess(type);
    }
}
